<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */

class b2c_ctl_admin_member_add extends desktop_controller{

    var $workground = 'b2c_ctl_admin_member';

    public function __construct($app)
    {
        parent::__construct($app);
        header("cache-control: no-store, no-cache, must-revalidate");
        $this->pagelimit = 15 ;
        $user = kernel::single('desktop_user');
        $this->username = $user->user_data['account']['login_name'];
        $this->user_id = $user->user_data['user_id'];
        $this->is_super = $user->user_data['super'];
    }
    function index(){
        $actions_base['title'] = app::get('b2c')->_('充值记录');
        if($this->is_super){
            $actions_base['base_filter'] = array('disabled' => 'false');
        }else{
            $store_id = $this->find_storeid_user($this->user_id);
            $actions_base['base_filter'] = array('disabled' => 'false','store_id'=>$store_id);
        }
        // $actions_base['actions'] = $custom_actions;
        $actions_base['use_buildin_recycle'] = false;
        $this->finder('b2c_mdl_member_shenhe',$actions_base);
    }

    function basic_setting(){
        // $user = kernel::single('desktop_user');
        // $username = $user->user_data['account']['login_name'];
        // $shenhe = app::get('b2c')->model('member_shenhe');
        // $nPage = $_GET['basic_setting'] ? $_GET['basic_setting'] : 1;
        // $mess = $shenhe->getList('*',array('operator_name'=>$username),$this->pagelimit*($nPage-1),$this->pagelimit,array('mtime',"DESC"));
        // $count = $shenhe->count(array('operator_name'=>$username));
        // $_GET['page'] = 'basic_setting';
        // $_GET['id'] = $username;
        // $this->pagination($nPage,$count,$_GET);
        // $this->pagedata['data'] = $mess;
        $this->page('admin/member/chongzhi.html');
    }

    public function ajax_html()
    {
        $finder_act = $_GET['finder_act'];
        $html = $this->$finder_act($_GET['id']);
        echo $html;
    }

    function member_mess(){
        if($_POST['member_mess']){
            $membersObj = app::get('pam')->model('members');
            $memberObj = app::get('b2c')->model('members');
            $member_id = $membersObj->getList('member_id',array('login_account'=>$_POST['member_mess'],'disabled'=>'false','login_type'=>'mobile'));
            if(!$member_id){
                $data1['DLname'] = '客户不存在';
                $data1['mobile'] = '客户不存在';
                $data1['remark'] = '客户不存在';
                $data1['name'] = '客户不存在';
                $result = json_encode($data1,true);
                echo $result;
                exit;
            }
            $login_name = $membersObj->getList('login_account',array('member_id'=>$member_id[0]['member_id'],'disabled'=>'false','login_type'=>'local'));
            $mess = $memberObj->getList('name,remark,mobile',array('member_id'=>$member_id[0]['member_id']));
            $data1['DLname'] = $login_name[0]['login_account'];
            $data1['mobile'] = $mess[0]['mobile'];
            $data1['remark'] = $mess[0]['remark'];
            $data1['name'] = $mess[0]['name'];
            $result = json_encode($data1,true);
            echo $result;
        }
    }

    function to_add(){
        $this->begin();
        $money = $_POST['money'];
        if($money <= 0){
            $this->end(false,app::get('b2c')->_('请输入有效金额'));
        }
        $mobile = $_POST['mobile'];
        //找会员
        $membersObj = app::get('pam')->model('members');
        $members = app::get('b2c')->model('members');
        $filter['login_type'] = 'mobile';
        $filter['login_account'] = $mobile;
        $filter['disabled'] = 'false';
        $member_id = $membersObj->getList('member_id',$filter);
        $member_id = $member_id[0]['member_id'];
        $big_credit = $members->getList('big_credit',array('member_id'=>$member_id));
        $big_credit = $big_credit[0]['big_credit'];
        if(!$member_id){
            $this->end(false,app::get('b2c')->_('会员查询失败，请联系财务'));
       }
        if($money){
            $user = kernel::single('desktop_user');
            $advanceObject = kernel::single('b2c_mdl_member_advance');       
            $advance = $advanceObject->get($member_id);
            if($advance < 0){
                $this->end(false, app::get('b2c')->_('更新预存款账户失败'));
            }
            if(!$this->username){
                $this->end(false, app::get('b2c')->_('错误！录入人查询失败！'));
            }
            //操作人员信息
            $message = $this->username.app::get('b2c')->_('店员代充值');
            $store_id = $this->find_storeid_user($this->user_id);
            #$total = $advance + $money;
            #$member['advance'] = $total;
            #$result = $adjmember->update($member,array('member_id'=>$member_id));
            // $result = $adjmember->db->exec('UPDATE sdb_b2c_members SET advance = advance+'.$money.' where member_id='.$member_id);
            $member_advance = $advanceObject->get($member_id);
            $current_shop_advance = $advanceObject->get_shop_advance();
            $shop_advance = $current_shop_advance+$money;
            $data = array(
                    'member_id'=>$member_id,
                    'member_name'=>$mobile,
                    'money'=>$money,
                    'yuzhi'=>$big_credit,
                    'message'=>$message,
                    'mtime'=>time(),
                    'payment_id'=>$payment_id,
                    'order_id'=>$order_id,
                    'paymethod'=>$paymenthod,
                    'memo'=>$memo,
                    'import_money'=>$money,
                    'explode_money'=>0,
                    'member_advance'=>$member_advance,
                    'shop_advance'=>$shop_advance,
                    'operator_name'=>$this->username,
                    'store_id'=>$this->is_super?'super':$store_id,
            );
            $shenhe = app::get('b2c')->model('member_shenhe');
            if($shenhe->save($data)){
                $this->end('success',app::get('b2c')->_('已提交，待审核'),'index.php?app=b2c&ctl=admin_member_add&act=basic_setting');
            }else{
                $this->end(false, app::get('b2c')->_('更新预存款帐户失败'));
            }
        }else{
            $this->end(false, app::get('b2c')->_('更新预存款帐户失败'));
        }
        
    }
    public function pagination($current,$count,$get){ //本控制器公共分页函数
        $app = app::get('b2c');
        $render = $app->render();
        $ui = new base_component_ui($this->app);
        // dump($current);
        // dump(ceil($count/$this->pagelimit));
        // dump($get);
        // exit();
        //unset($get['singlepage']);
        $link = 'index.php?app=b2c&ctl=admin_member_add&act=ajax_html&id='.$get['id'].'&finder_act='.$get['page'].'&'.$get['page'].'=%d';
        $this->pagedata['pager'] = $ui->pager(array(
                'current'=>$current,
                'total'=>ceil($count/$this->pagelimit),
                'link' =>$link,
            ));
        return $this->pagedata['pager'];
    }

    public function find_storeid_user($user_id){
        $hasroleObj = app::get('desktop')->model('hasrole');
        $rolesObj = app::get('desktop')->model('roles');
        $role_id = $hasroleObj->getList('role_id',array('user_id'=>$user_id));
        $store_id = $rolesObj->getList('store_id',array('role_id'=>$role_id[0]['role_id']));
        return $store_id[0]['store_id'];
    }
}
